﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 创建者：zhaijc
 * 创建时间：2023/4/14 11:22:36
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>

using DotNetty.Buffers;
using DotNetty.Codecs;
using DotNetty.Transport.Channels;
using MedianSocketService.services;
using MedianSocketService.utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace MedianSocketService.netty.MedianProtocol
{
    public class MedianProtocolHandler : ChannelHandlerAdapter
    {
        SocketClientInfo sc;
        public Action<SocketClientInfo> OnConnectedCompletedDelegate;
        public Action<SocketClientInfo> OnDisConnectedDelegate;
        public Action<SocketClientInfo, byte[]> OnReceiveCompletedDelegate;
        private MedianProtocolService medianProtocolService;

        public MedianProtocolHandler()
        {

        }

        public override void ChannelActive(IChannelHandlerContext context)
        {
            // 获取远程地址
            IPAddress remoteAddr = ((IPEndPoint)context.Channel.RemoteAddress).Address;
            int remotePort = ((IPEndPoint)context.Channel.RemoteAddress).Port;
            ChannelManager channelManager = ChannelManager.GetInstance();
            sc = new SocketClientInfo() { Ip = remoteAddr.MapToIPv4().ToString(), Port = remotePort };
            //加入到通道管理中
            channelManager.AddChannel(sc, context.Channel);
            //中位机协议
            medianProtocolService = new MedianProtocolService();
            medianProtocolService.sci = sc;

            //通知连接成功
            if (OnConnectedCompletedDelegate != null)
            {
                OnConnectedCompletedDelegate(sc);
            }
            base.ChannelActive(context);

        }

        public override void ChannelInactive(IChannelHandlerContext context)
        {
            ChannelManager channelManager = ChannelManager.GetInstance();
            channelManager.RemoveChannel(sc);

            if (OnDisConnectedDelegate != null)
            {
                OnDisConnectedDelegate(sc);
            }

            base.ChannelInactive(context);
        }

        public override void ChannelRead(IChannelHandlerContext context, object message)
        {

            byte[] resultByte = (byte[])message;
            //通知
            if (OnReceiveCompletedDelegate != null)
            {
                OnReceiveCompletedDelegate(sc, resultByte);
            }
            //解包
            medianProtocolService.UnPack(resultByte);
           
            
        }

        public override void ChannelReadComplete(IChannelHandlerContext context)
        {
            base.ChannelReadComplete(context);
        }

        public override void ChannelRegistered(IChannelHandlerContext context)
        {
            base.ChannelRegistered(context);
        }

        public override void ChannelUnregistered(IChannelHandlerContext context)
        {
            //从通道管理中移除
            ChannelManager channelManager = ChannelManager.GetInstance();
            channelManager.RemoveChannel(sc);
            base.ChannelUnregistered(context);
        }

        public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
        {
            if (exception is CorruptedFrameException)
            {
                // 处理包头校验失败异常
                // ...
            }
            else if (exception is TooLongFrameException)
            {
                // 处理包体过长异常
                // ...
            }
            else
            {
                // 其他异常处理
                // ...
            }
        }

    }
}
